package com.scu.bank.web;
import com.scu.bank.core.jo.Result;
import com.scu.bank.core.jo.ResultGenerator;
import com.scu.bank.model.pojo.UserInfo;
import com.scu.bank.service.UserInfoService;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by pmeng on 2020/05/26.
*/
@RestController
@RequestMapping("/user/info")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/add")
    public Result add(@RequestBody UserInfo userInfo) {
        userInfoService.addUserInfo(userInfo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody List<String> ids) {
        if (ids == null){
            return ResultGenerator.genFailResult("不能为空");
        }
        userInfoService.deleteByIds(ids);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody UserInfo userInfo) {
        userInfoService.update(userInfo);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String id) {
        UserInfo userInfo = userInfoService.getById(id);
        return ResultGenerator.genSuccessResult(userInfo);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageInfo<UserInfo> pageInfo = userInfoService.list(page, size);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/restPwd")
    public Result restPwd(String id,String pwd) {
        userInfoService.restPwd(id,pwd);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/setRole")
    public Result setRole(String uId,String roleId){
        userInfoService.setRole(uId,roleId);
        return ResultGenerator.genSuccessResult();
    }
}
